package com.utils.info.controller;

import com.utils.info.util.hhy.QRCodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;

/**
 * @description 二维码控制器
 */
@Controller
public class QrcodeController {

    /**
     * @param url     跳转地址
     * @param imgPath 图片
     * @description 生成二维码
     * @Param response
     **/
    @GetMapping("/getQrcode")
    @ResponseBody
    public void getQrcode(String url, String imgPath, HttpServletResponse response) throws Exception {
        Boolean needCompress = true;
        //拿到图片流
        ByteArrayOutputStream out = QRCodeUtil.encodeIO(url, imgPath, needCompress);
        //返回二维码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("image/jpeg;charset=UTF-8");
        response.setContentLength(out.size());
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(out.toByteArray());
        outputStream.flush();
        outputStream.close();
    }

}